package mt.dummy;

import java.awt.Component;
import java.util.ArrayList;

import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableCellRenderer;

/**
 * Creates the table used in MicroTraderClientDummy, using the java class AbstractTableModel
 */

public class ClientTableModel extends AbstractTableModel{	

	ArrayList<Object[]> oL;
	String[] header;	

	ClientTableModel(Object[] obj, String[] header) {		

		this.header = header;
		oL = new ArrayList<Object[]>();
		
			//	oL.add(obj);
	}	

	public int getRowCount() {
		return oL.size();
	}

	public int getColumnCount() {
		return header.length;
	}

	public Object getValueAt(int rowIndex, int columnIndex) {
		return oL.get(rowIndex)[columnIndex];
		
	}

	public String getColumnName(int index) {
		return header[index];
	}
	
	void add(String stock, int numberOfUnits, double pricePerUnit, boolean buyOrder, int idOrder) {
		boolean foundStock = false;
				
		for(int i = 0; i<oL.size();i++){			
			if (oL.get(i)[4].equals(String.valueOf(idOrder))){
				foundStock = true;
				oL.get(i)[1]= String.valueOf(numberOfUnits);
				if (numberOfUnits==0){
					oL.remove(i);
				}
				break;
			}
		}
		if(!foundStock){
		String[] str = new String[5];
		str[0] = stock;
		str[1] = String.valueOf(numberOfUnits);
		str[2] = String.valueOf(pricePerUnit);
		
		if(buyOrder){
			str[3] = "BUY";
		}
		else{
			str[3] = "SELL";
		}
		str[4]= String.valueOf(idOrder);
		oL.add(str);
		}
		// inform the GUI that I have changed the list information.
		fireTableDataChanged();
		
	}
}